package com.keba.lamp.complaint.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Multimap;
import com.keba.basic.base.R;
import com.keba.basic.base.controller.SuperController;
import com.keba.basic.interfaces.echo.EchoService;
import com.keba.lamp.common.constant.AppendixType;
import com.keba.lamp.complaint.entity.EvidenceComponent;
import com.keba.lamp.complaint.service.EvidenceComponentService;
import com.keba.lamp.complaint.vo.query.EvidenceComponentPageQuery;
import com.keba.lamp.complaint.vo.result.EvidenceComponentResultVO;
import com.keba.lamp.complaint.vo.save.EvidenceComponentSaveVO;
import com.keba.lamp.complaint.vo.update.EvidenceComponentUpdateVO;
import com.keba.lamp.file.service.AppendixService;
import com.keba.lamp.file.service.FileService;
import com.keba.lamp.model.vo.result.AppendixResultVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * 证据组件库
 * </p>
 *
 * @author chaowu.wang
 * @date 2025-03-27 20:34:20
 * @create [2025-03-27 20:34:20] [chaowu.wang] [代码生成器生成]
 */
@Slf4j
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/evidenceComponent")
@Tag(name = "证据组件库")
public class EvidenceComponentController extends SuperController<EvidenceComponentService, Long, EvidenceComponent
    , EvidenceComponentSaveVO, EvidenceComponentUpdateVO, EvidenceComponentPageQuery, EvidenceComponentResultVO> {
    private final EchoService echoService;
    private final AppendixService appendixService;
    private final FileService fileService;
    @Override
    public EchoService getEchoService() {
        return echoService;
    }

    @Override
    public R<EvidenceComponentResultVO> getDetail(Long id) {
        EvidenceComponentResultVO resultVO = this.superService.getComponentById(id);
        echoService.action(resultVO);
        return success(resultVO);
    }

    @Override
    public R<Boolean> handlerDelete(List<Long> ids) {
        return R.success(superService.delete(ids));
    }

    @Override
    public void handlerResult(IPage<EvidenceComponentResultVO> page) {
        super.handlerResult(page);
        List<EvidenceComponentResultVO> records = page.getRecords();
        if (CollectionUtil.isNotEmpty(records)) {
            List<Long> bizIdSet = records.stream().map(EvidenceComponentResultVO::getId).collect(Collectors.toList());
            Multimap<AppendixService.AppendixBizKey, AppendixResultVO> map = appendixService.listByBizIds(bizIdSet, AppendixType.Complaint.COMPLAINT__EVIDENCE__SAMPLE__FILE);
            records.forEach(resultVO -> {
                AppendixResultVO appendixResultVO = map.get(new AppendixService.AppendixBizKey(resultVO.getId(), AppendixType.Complaint.COMPLAINT__EVIDENCE__SAMPLE__FILE)).stream().findFirst().orElse(null);
                if (Objects.nonNull(appendixResultVO)) {
                    String url = fileService.findUrlById(appendixResultVO.getId());
                    resultVO.setSamplePicUrl(url);
                }
            });
        }
    }
}


